#!/bin/bash
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   runtest.sh of /tools/systemtap/Regression/small-tests
#   Description: suns small tests
#   Author: Martin Cermak <mcermak@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2014 Red Hat, Inc.
#
#   This program is free software: you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation, either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE.  See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see http://www.gnu.org/licenses/.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1

PACKAGE="systemtap"

export DEBUGINFOD_URLS="https://debuginfod.fedoraproject.org/ $DEBUGINFOD_URLS"
export DEBUGINFOD_TIMEOUT=300

_arch=$(arch)

function perf_probe_failed ()
{
    probe=$1
    perf probe -d $probe ||:
    perf probe --add $probe
    retval=$?
    test $retval -eq 0 && \
        rlLogInfo "Running perf probe --add $probe PASSED" || \
        rlLogInfo "Running perf probe --add $probe FAILED"
    perf probe -d $probe ||:
    if test $retval -eq 0; then
        return 1
    else
        return 0
    fi
}

function perf_found_none_of ()
{
    ret=0
    for f in "$@"; do
        perf_probe_failed $f || ret=1
    done
    return $ret
}

rlJournalStart
    # Check the environment ---------------------------------------
    rlPhaseStart WARN "Check the environment"
        rlRun "rpm -qa | egrep '^(kernel|systemtap|perf)' | sort"
        rlRun "uname -r"
        rlRun "perf --version"
    rlPhaseEnd

    # bz1162939 ---------------------------------------------------
    (
        perf_probe_failed "vfs_read" && exit
        rlPhaseStart FAIL "test 1 bz1162939"
            # At this point a weird problem shows up:  sometimes the first
            # systemtap invocation fails for some reason, but subsequent
            # invocations succeed.  Can't reproduce this manually:
            rlRun "stap -vvvp4 -e 'probe vfs.read{println(\"hit\")}'" 0,1
            sleep 30
            rlRun "stap -vvvp4 -e 'probe vfs.read{println(\"hit\")}'" 0,1
            sleep 30
            rlRun "stap -e 'probe vfs.read{println(\"hit\")}' -c 'head -1 /etc/hosts'"
        rlPhaseEnd
    )
    # bz1119335 ---------------------------------------------------
    rlPhaseStart FAIL "test 2 bz1119335"
        rlRun "man 8 staprun | grep 'STAP_FIPS_OVERRIDE'"
    rlPhaseEnd

    # bz1153673 ---------------------------------------------------
    rlPhaseStart FAIL "test 3 bz1153673"
        _tested=0
        for _pkg in $( rpm -qa | grep systemtap-testsuite ); do
            for _tc in $( rpm -ql $_pkg | grep 'alias_suffixes.stp$' ); do
                stap -p4 $_tc && p="" || p="-P"
                rlRun "stap $p -p4 $_tc"
                _tested=1
            done
        done
        [[ $_tested -eq 0 ]] && rlFail "Not tested"
    rlPhaseEnd

    # bz1160837 ---------------------------------------------------
    (
        perf_probe_failed "sd_init_command" && exit
        rlPhaseStart FAIL "test 4 bz1160837"
            _tested=0
            _prologue_search=''
            for _pkg in $( rpm -qa | grep systemtap-testsuite ); do
                for _tc in $( rpm -ql $_pkg | grep 'iostat-scsi.stp$' ); do
                    test $_arch = ppc64le && _prologue_search='-P'
                    rlRun "stap -g $_prologue_search $_tc 1 -c 'sleep 1'"
                    _tested=1
                done
            done
            [[ $_tested -eq 0 ]] && rlFail "Not tested"
        rlPhaseEnd
    )

    # bz1203808 ---------------------------------------------------
    rlPhaseStart FAIL "test 5 bz1203808"
            _tested=0
            for _pkg in $( rpm -qa | grep systemtap-testsuite ); do
                for _tc in $( rpm -ql $_pkg | grep 'vfs-all-probes.stp$' ); do
                    stap -wp4 $_tc && p="" || p="-P"
                    rlRun "stap $p -w -p4  $_tc"
                    _tested=1
                done
            done
            [[ $_tested -eq 0 ]] && rlFail "Not tested"
    rlPhaseEnd

    # bz1269062 ---------------------------------------------------
    rlPhaseStart FAIL "test 6 bz1269062"
        rlRun "stap -c sync --suppress-handler-errors -e 'probe ioscheduler.elv_add_request{println(elevator_name) exit()}'"
    rlPhaseEnd

    # bz528792 ---------------------------------------------------
    (
        # Not relevant on rhel-9 because of kernel commit cad6967ac10843a70842cd39c7b53412901dd21f that removes
        # _do_fork() and replaces it with new kernel_clone()
        perf_found_none_of "_do_fork" "do_fork" "sys_clone" && exit

        rlPhaseStart FAIL "test 7 bz528792"
            if test $_arch = ppc64le; then
                # https://bugzilla.redhat.com/show_bug.cgi?id=1652740#c4  - We can not reach any function parameters, namely $clone_flags
                rlRun "stap -ve 'probe kernel.function(\"_do_fork\")!, kernel.function(\"do_fork\")!, kernel.function(\"sys_clone\") { println(pp()) exit() }' -c \"bash -c 'while sleep 1; do /bin/true; done'\""
            else
                rlRun "stap -ve 'probe kernel.function(\"_do_fork\")!, kernel.function(\"do_fork\")!, kernel.function(\"sys_clone\") { printf(\"%x\n\",\$clone_flags) exit() }' -c \"bash -c 'while sleep 1; do /bin/true; done'\""
            fi
        rlPhaseEnd
    )

    # bz1890702 ---------------------------------------------------
    (
        perf_probe_failed "vfs_open" && exit
        rlPhaseStart FAIL "test 8 bz1890702"
            rlRun "stap -g --suppress-time-limits -e 'probe kernel.function(\"vfs_open\") { printf(\"%s(path: %s)\", ppfunc(), \$path\$) ; exit() }' -c 'cat /etc/hosts'"
        rlPhaseEnd
    )

    # bz1904216 ---------------------------------------------------
    rlPhaseStart FAIL "test 9 bz1904216"
        rlRun "stap -p4 -DSTP_NO_OVERLOAD -t -c 'sleep 0.25' -e 'probe kernel.trace(\"bcache:bcache_btree_set_root\") { if (pid() == 0) printf(\"probe hit\n\"); }'"
    rlPhaseEnd

    # bz1940804 ---------------------------------------------------
    rlPhaseStart FAIL "test 10 bz1940804"
        rlRun "stap -e 'probe process(\"/bin/bash\").function(\"main\") {println(pn()) }' -c '/bin/bash --help'"
    rlPhaseEnd

    # bz1940945 ---------------------------------------------------
    rlPhaseStart FAIL "test 11 bz1940945"
        rlRun "stap -c sync --suppress-handler-errors -e 'probe ioscheduler.elv_add_request{println(elevator_name) exit()}'"
    rlPhaseEnd

    # bz1940761 ---------------------------------------------------
    rlPhaseStart FAIL "test 12 bz1940761"
        rlRun "stap -v /usr/share/systemtap/examples/process/strace.stp -w -c \"echo hello world\""
    rlPhaseEnd
rlJournalPrintText
rlJournalEnd
